/***************************************************************************
 *   Copyright (C) 2007 by Phillip Aldridge   *
 *   phillip@imi21.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "iconbanner.h"
#include "config.h"
#include "stationdata.h"

#include <qfont.h>
#include <qcolor.h>
#include <qpen.h>
#include <qdatetime.h>

/**
   This draws the Web banner which can be uploaded to a site
   TODO Better graphics
   TODO The user should be able to choose colours or use a template image
   TODO Save and FTP upload to site or other location
*/
IconBanner::IconBanner(QWidget *parent, const char *name)
 : QWidget(parent, "iconBanner")
{
   qDebug("Setting up View");
   width=500; height=100;
   setFixedSize (width,height);
}


IconBanner::~IconBanner()
{
}
void IconBanner::setStationData(StationData *s)
{
   stationdata=s;
}
void IconBanner::save()
{
   const QString file=QString("%1/%2").arg(_FTP_LOCAL_FILES).arg(_FTP_BANNER);
   QPixmap pm( width, height );
   bitBlt( &pm,0,0, this, 0, 0, -1,-1 );
   pm.save(file,"PNG",80);
}
void IconBanner::paintEvent( QPaintEvent *e )
{ 
   QPainter painter(this);
   QPen pen = painter.pen();
   QString ht;
   painter.fillRect(0, 0, width, height, Qt::green);

int c1=5, c2=105, c3=210, c4=320, c5=405;
int r1=30, r2=45, r3=60, r4=75, r5=95;

   QFont ft( "Helvetica", 10 );
   painter.setFont( ft );
   pen.setColor(Qt::black);
   painter.setPen( pen );

   ht= QString("%1 %2").arg(_BANNER_TITLE)
         .arg(QDateTime::currentDateTime()
         .toString(QString(("yyyy-MM-dd hh:mm:ss"))));
   painter.drawText( c1, 13, ht );
   QFont fc( "Helvetica", 8 );
   painter.setFont( fc );
   ht= tr("Temperature");
   painter.drawText( c1, r1, ht );
   ht= tr("Now : %1%2").arg(stationdata->tempOut() ).arg(_UNIT_TEMP);
   painter.drawText( c1, r2, ht );
   ht= tr("Max : %1%2").arg(stationdata->tempMax() ).arg(_UNIT_TEMP);
   painter.drawText( c1, r3, ht );
   ht= tr("Min : %1%2").arg(stationdata->tempMin() ).arg(_UNIT_TEMP);
   painter.drawText( c1, r4, ht );
 ht= tr("Wind");
   painter.drawText( c2, r1, ht );
   ht= tr("Speed : %1%2").arg(stationdata->windSpeed() ).arg(_UNIT_SPEED);
   painter.drawText( c2, r2, ht );
   ht= tr("Direction : %1 %2%3").arg(sdWindDirection).arg(stationdata->windAngle()).arg(_UNIT_TEMP);
   painter.drawText( c2, r3, ht );
   ht= tr("Chill : %1%2").arg(stationdata->windChill()).arg(_UNIT_TEMP);
   painter.drawText( c2, r4, ht );

   ht= tr("Pressure");
   painter.drawText( c3, r1, ht );
   ht= tr("ABS : %1%2").arg(stationdata->absPressure() ).arg(_UNIT_PRESSURE);
   painter.drawText( c3, r2, ht );
   ht= tr("Rel : %1%2").arg(stationdata->relPressure() ).arg(_UNIT_PRESSURE);
   painter.drawText( c3, r3, ht );
   ht= tr("Hum : %1%2").arg(stationdata->relHumOut() ).arg(_UNIT_HUMIDITY);
   painter.drawText( c3, r4, ht );

   ht= tr("Rain");
   painter.drawText( c4, r1, ht );
   ht= tr("1h : %1%2").arg(stationdata->rain1h() ).arg(_UNIT_RAIN);
   painter.drawText( c4, r2, ht );
   ht= tr("24h : %1%2").arg(stationdata->rain24h() ).arg(_UNIT_RAIN);
   painter.drawText( c4, r3, ht );
   ht= tr("1w : %1%2").arg(stationdata->rain1w() ).arg(_UNIT_RAIN);
   painter.drawText( c4, r4, ht );
   ht= tr("1m : %1%2").arg(stationdata->rain1m() ).arg(_UNIT_RAIN);
   painter.drawText( c5, r3, ht );
   ht= tr("Y : %1%2").arg(stationdata->rainTotal() ).arg(_UNIT_RAIN);
   painter.drawText( c5, r4, ht );

   QFont fb( "Helvetica", 9 );
   painter.setFont( fb );
   ht= QString(_BANNER_COPYRIGHT);
   painter.drawText( c4, r5, ht );
   ht= QString("GPS %1").arg( _BANNER_GPS);
   painter.drawText( c1, r5, ht );

save();
}


